import Vue from 'vue'
import './plugins/axios'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false
let musicStore = {
  musicID: null,
  musicList: [],
  currentMusic: null,
}

new Vue({
  data: {
    musicStore,
    lyric: null,
    searchHistory:[],
  },
  watch: {
    searchHistory: function(){
      let newArr=[...new Set(this.searchHistory)];
      localStorage.setItem("searchHistory",JSON.stringify(newArr));
    },
    musicStore: {
      handler() {
        let musicID = this.musicStore.musicID;
        this.axios.get('http://music.kele8.cn/lyric?id=' + musicID).then(data => {
          if (data.data.nolyric) {
            this.lyric=[{
             lyric:"纯音乐欣赏~",
             time:0,
            }];
          } else {
            let lyricArr = data.data.lrc.lyric.split('\n');
            lyricArr.pop();
            let reg = /\[(\d+):(\d+\.\d+)\](.*)/;
            let newLiric = lyricArr.map(d => {
              if (reg.test(d)) {
                return {
                  time: (parseInt(RegExp.$1) * 60) + parseFloat(RegExp.$2),
                  lyric: RegExp.$3
                }
              }
            });
            this.lyric = newLiric;
          }
        });
      },
      deep: true
    }

  },
  router,
  render: h => h(App)
}).$mount('#app')
